import { Component,ViewChild,ElementRef } from "@angular/core";
import { NavController,NavParams, AlertController } from 'ionic-angular'
import { HttpClient,HttpHeaders } from '@angular/common/http'
import { NativeStorage } from '@ionic-native/native-storage';
import { LaunchNavigator } from '@ionic-native/launch-navigator';

import { ContactPage } from "../../contact/contact";
import { SlookPage } from "../../contact/slook/slook";
import { PayPage } from "../../pay/pay";
import { AppConfig } from '../../../app/app.config';
declare var google;
@Component({
    selector: 'page-product',
    templateUrl: 'service.html',
})
export class ProductservicePage {
    @ViewChild('map') mapElement: ElementRef;
    mapss:any;
    contactPage =ContactPage;
    slookPage = SlookPage;
    payPage = PayPage;
    alert_active=false;
    report_active=false;
    token:string
    info={
        cate:'',
        logo:'',
        logos:'',
        address:'',
        price_unit:'',
        price_type:1,
        price:'',
        deposit_unit:1,
        deposit_price:'',
        tax_unit:'',
        tax_price:'',
        floor:'',
        total_floor:'',
        owner_tel:'',
        description:'',
        owner_email:'',
        collected:true,
        video:'',
        autoorder:1
    };
    url_pre:string;
    evaluate_list=[]
    toCancat:any
    latlng = []
    constructor(
        private http: HttpClient,
        private navParams:NavParams,
        private alertCtrl:AlertController,
        private stroge:NativeStorage,
        private launchNavigator:LaunchNavigator,
        private navCtl:NavController
    ) {
        
    }
    ngAfterViewInit() {
        let id = this.navParams.get('id');
        this.http.get(AppConfig.getUrl()+'service/info?id='+id).subscribe(res=>{
            if(res['code']==101){
                this.info = res['data']
                this.toCancat = res['data']
                this.info.collected = res['data']['collect']?true:false;
                this.latlng = [res['data']['latitude'],res['data']['longitude']]
            }
        })
        this.stroge.getItem('token').then(resp=>{
            this.token = resp
            
            this.http.get(AppConfig.getUrl()+'Cart/getComments?pid='+id+'&token='+resp+'&modal=service').subscribe(res=>{
                if(res['code']==101){
                    this.evaluate_list = res['data']
                }
            })
        })
    }
    alertb(title,subTitle){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: ['OK']
        });
        alert.present();
    }
    collect(){
        this.http.post(AppConfig.getUrl()+'service/collection?token='+this.token, AppConfig.urlEcode({pid:this.info['id']}),{headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }) })
            .subscribe( (res) => {
                if(res['code']===101){
                    this.info['collect'] = !this.info['collect']
                }else if(res['code']===202){
                    this.alertb('Error!',res['msg']);
                }
            })
    }
    _unit(item:number){
        switch(item){
            case 1:
                return '£'
            case 2:
                return '€'
            case 3:
                return '$'
            case 4:
                return '¥'
        }
    }
    navto(){
        this.launchNavigator.navigate(this.latlng)
        .then(
            success => console.log('Launched navigator'),
            error => console.log('Error launching navigator', error)
        );
    }
    gopay(){
        if(this.info.autoorder == 1){
            this.navCtl.push(this.payPage,{data:this.toCancat})
        }else if(this.info.autoorder == -1){
            this.navCtl.push(this.contactPage,{data:this.toCancat})
        }
    }
}